Imports SchemaCodeGen

Partial Class pages_Connections_Connection : Inherits CPage

#Region "Querystring"
	Public ReadOnly Property ConnectionId() As Integer
		Get
			Return CWeb.RequestInt("connectionId")
		End Get
	End Property
	Public ReadOnly Property IsEdit() As Boolean
		Get
			Return ConnectionId <> Integer.MinValue
		End Get
	End Property
#End Region

#Region "Members"
	Private m_connection As CConnection
#End Region

#Region "Data"
	Public ReadOnly Property [Connection]() As CConnection
		Get
			If IsNothing(m_connection) Then
				If IsEdit Then
					m_connection = CConnection.Cache.GetById(ConnectionId)
					If IsNothing(m_connection) Then CSitemap.RecordNotFound("Connection", ConnectionId)
				Else
					m_connection = New CConnection
					'Inserts: set parentId here (if applicable)
				End If
			End If
			Return m_connection
		End Get
	End Property
#End Region

#Region "Navigation"
	Private Sub Refresh()
		Response.Redirect(CSitemap.ConnectionEdit(Me.Connection.ConnectionId))
	End Sub
	Private Sub ReturnToList()
		Response.Redirect(CSitemap.Connections)
	End Sub
#End Region

#Region "Event Handlers - Page"
	Protected Overrides Sub PageInit()
		With ddConnectionSetId
			.DataTextField = "SetName_"
			.DataValueField = "SetId"
			.DataSource = CTemplateSet.Cache
			.DataBind()
		End With


		With ddConnectionDriverId
			.DataTextField = "DriverName"
			.DataValueField = "DriverId"
			.DataSource = CDriverType.Cache
			.DataBind()
		End With



		'Page Title
		If IsEdit Then
			Me.Title = "Project Details"
		Else
			Me.Title = "Create New Project"
			flHistory.Visible = False
			txtConnectionUserName.Visible = False
			txtConnectionCreated.Visible = False
		End If

		'Textbox logic
		'Me.Form.DefaultFocus = txtConnectionName.Textbox.ClientID;
		Me.Form.DefaultButton = btnSave.UniqueID   'txtConnectionName.OnReturnPress(btnSave)

		'Button Text
		btnDelete.Visible = IsEdit
		If IsEdit Then btnCancel.Text = "Back" Else btnSave.Text = "Create Project"
		If IsEdit Then AddButton(CSitemap.ConnectionAdd(), "Create a new Project")
	End Sub
	Protected Overrides Sub PageLoad()
		If Page.IsPostBack Then Exit Sub

		LoadConnection()
	End Sub
#End Region

#Region "Event Handlers - Form"
	Private Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
		If Not Me.IsValid() Then Exit Sub
		SaveConnection()
		Refresh()
	End Sub
	Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
		ReturnToList()
	End Sub
	Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
		Me.Connection.Delete()
		'CCache.ClearCache()
		ReturnToList()
	End Sub
#End Region

#Region "Private - Load/Save"
	Protected Sub LoadConnection()
		With Me.Connection
			txtConnectionName.Text = .ConnectionName
			ddConnectionSetId.ValueInt = .ConnectionSetId
			txtConnectionUserName.Text = .ConnectionUserName
			ddConnectionDriverId.ValueInt = .ConnectionDriverId
			txtConnectionString.Text = .ConnectionString
			txtConnectionPassword.Text = .ConnectionPassword
			txtConnectionOutputDir.Text = .ConnectionOutputDir
			txtConnectionNameSpace.Text = .ConnectionNameSpace
			chkConnectionUseAuditTrail.Checked = .ConnectionUseAuditTrail
			chkConnectionUseCaching.Checked = .ConnectionUseCaching
			txtConnectionTablePrefix.Text = .ConnectionTablePrefix
			txtConnectionCreated.ValueDate = .ConnectionCreated
		End With
	End Sub
	Protected Sub SaveConnection()
		With Me.Connection
			.ConnectionName = txtConnectionName.Text
			.ConnectionSetId = ddConnectionSetId.ValueInt
			.ConnectionUserName = txtConnectionUserName.Text
			.ConnectionDriverId = ddConnectionDriverId.ValueInt
			.ConnectionString = txtConnectionString.Text
			.ConnectionPassword = txtConnectionPassword.Text
			.ConnectionOutputDir = txtConnectionOutputDir.Text
			.ConnectionNameSpace = txtConnectionNameSpace.Text
			.ConnectionUseAuditTrail = chkConnectionUseAuditTrail.Checked
			.ConnectionUseCaching = chkConnectionUseCaching.Checked
			.ConnectionTablePrefix = txtConnectionTablePrefix.Text
			.ConnectionCreated = txtConnectionCreated.ValueDate

			.Save()
		End With
		'CCache.ClearCache()
	End Sub
#End Region


End Class
